package no.uib.prideconverter.gui;

import no.uib.prideconverter.PRIDEConverter;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import no.uib.prideconverter.filefilters.DatFileFilter;
import no.uib.prideconverter.filefilters.DtaFileFilter;
import no.uib.prideconverter.filefilters.MgfFileFilter;
import no.uib.prideconverter.filefilters.Ms2FileFilter;
import no.uib.prideconverter.filefilters.MzDataFileFilter;
import no.uib.prideconverter.filefilters.MzMlFileFilter;
import no.uib.prideconverter.filefilters.MzXmlFileFilter;
import no.uib.prideconverter.filefilters.OmxFileFilter;
import no.uib.prideconverter.filefilters.PklFileFilter;
import no.uib.prideconverter.filefilters.PkxFileFilter;
import no.uib.prideconverter.filefilters.XmlFileFilter;
import uk.ac.ebi.pride.model.interfaces.mzdata.CvParam;
import uk.ac.ebi.pride.model.interfaces.mzdata.UserParam;

/**
 * This frame handles the selection of the data files to be converted for 
 * dataformats with only on file per spectrum + identification.
 * 
 * @author Harald Barsnes
 * 
 * Created March 2008
 */
public class DataFileSelection extends javax.swing.JFrame {


    /** 
     * Opens a new DataFileSelection frame, and inserts stored information.
     * 
     * @param location where to position the frame
     */
    public DataFileSelection(Point location) {

        // sets the default wizard frame size
        this.setPreferredSize(new Dimension(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT));
        this.setSize(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT);
        this.setMaximumSize(new Dimension(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT));
        this.setMinimumSize(new Dimension(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT));

        initComponents();

        selectedFilesJTable.getTableHeader().setReorderingAllowed(false);
        selectedFilesJTable.getColumn(" ").setMinWidth(40);
        selectedFilesJTable.getColumn(" ").setMaxWidth(40);
        selectedFilesJTable.setRowHeight(selectedFilesJTable.getFont().getSize() + PRIDEConverter.getProperties().tableRowHeightPaddingSize);

        // insert stored information
        if (PRIDEConverter.getProperties().getSelectedSourceFiles() != null) {

            for (int i = 0; i < PRIDEConverter.getProperties().getSelectedSourceFiles().size(); i++) {

                ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                        new Object[]{
                            new Integer(selectedFilesJTable.getRowCount() + 1),
                            (new File(PRIDEConverter.getProperties().getSelectedSourceFiles().get(i))).getName()
                        });
            }

            nextJButton.setEnabled(selectedFilesJTable.getRowCount() > 0);
        } else {
            PRIDEConverter.getProperties().setSelectedSourceFiles(new ArrayList());
        }

        // sets the icon of the frame
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().
                getResource("/no/uib/prideconverter/icons/prideConverter_16.GIF")));

        setTitle(PRIDEConverter.getWizardName() + " " +
                PRIDEConverter.getPrideConverterVersionNumber() + " - " + getTitle());

        if (location != null) {
            setLocation(location);
        } else {
            setLocationRelativeTo(null);
        }

        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        deleteSelectedFileJPopupMenu = new javax.swing.JPopupMenu();
        deleteJMenuItem = new javax.swing.JMenuItem();
        nextJButton = new javax.swing.JButton();
        backJButton = new javax.swing.JButton();
        cancelJButton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        selectedFilesJTable = new javax.swing.JTable();
        selectFileOrFolderJButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        helpJButton = new javax.swing.JButton();
        aboutJButton = new javax.swing.JButton();

        deleteJMenuItem.setText("Delete Selected Row(s)");
        deleteJMenuItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteJMenuItemActionPerformed(evt);
            }
        });
        deleteSelectedFileJPopupMenu.add(deleteJMenuItem);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("File Selection - Step 1 of 8");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        nextJButton.setText("Next  >");
        nextJButton.setEnabled(false);
        nextJButton.setPreferredSize(new java.awt.Dimension(81, 23));
        nextJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextJButtonActionPerformed(evt);
            }
        });

        backJButton.setText("< Back");
        backJButton.setPreferredSize(new java.awt.Dimension(81, 23));
        backJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backJButtonActionPerformed(evt);
            }
        });

        cancelJButton.setText("Cancel");
        cancelJButton.setPreferredSize(new java.awt.Dimension(81, 23));
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("File Selection"));

        jLabel4.setText("Selected Files:");

        selectedFilesJTable.setFont(selectedFilesJTable.getFont());
        selectedFilesJTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                " ", "File"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        selectedFilesJTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                selectedFilesJTableMouseClicked(evt);
            }
        });
        selectedFilesJTable.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                selectedFilesJTableKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(selectedFilesJTable);

        selectFileOrFolderJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/Directory.gif"))); // NOI18N
        selectFileOrFolderJButton.setText("Select a File or a Folder");
        selectFileOrFolderJButton.setHorizontalTextPosition(javax.swing.SwingConstants.LEADING);
        selectFileOrFolderJButton.setMaximumSize(new java.awt.Dimension(167, 23));
        selectFileOrFolderJButton.setMinimumSize(new java.awt.Dimension(167, 23));
        selectFileOrFolderJButton.setPreferredSize(new java.awt.Dimension(167, 23));
        selectFileOrFolderJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectFileOrFolderJButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, selectFileOrFolderJButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel4))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .add(selectFileOrFolderJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel3.setFont(jLabel3.getFont().deriveFont((jLabel3.getFont().getStyle() | java.awt.Font.ITALIC), jLabel3.getFont().getSize()-2));
        jLabel3.setText("Select the files you want to convert data from, and click on 'Next'  to continue.");

        helpJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/help.GIF"))); // NOI18N
        helpJButton.setToolTipText("Help");
        helpJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpJButtonActionPerformed(evt);
            }
        });

        aboutJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/prideConverter_16.GIF"))); // NOI18N
        aboutJButton.setToolTipText("About");
        aboutJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutJButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(helpJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(aboutJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 239, Short.MAX_VALUE)
                        .add(backJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(nextJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(cancelJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 562, Short.MAX_VALUE)
                    .add(jLabel3))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(18, 18, 18)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(7, 7, 7)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(cancelJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(nextJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(backJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(helpJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(aboutJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Closes the frame and the wizard.
     * 
     * @param evt
     */
    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJButtonActionPerformed
        this.setVisible(false);
        this.dispose();
        PRIDEConverter.cancelConvertion();
    }//GEN-LAST:event_cancelJButtonActionPerformed

    /**
     * Closes the frame and opens the next frame (SpectraSelection).
     * 
     * @param evt
     */
    private void nextJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextJButtonActionPerformed

        boolean cancel = false;

        // for mzData you can only convert one file at the time
        if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("mzData")) {

            if (selectedFilesJTable.getRowCount() > 1) {
                JOptionPane.showMessageDialog(this,
                        "For mzData you can only convert one file at the time.\n" +
                        "Reduce the selected set of files before continuing.",
                        "mzData Conversion Limitation",
                        JOptionPane.WARNING_MESSAGE);
                cancel = true;
            }
        }

        if (!cancel) {

            this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

            if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("SEQUEST DTA File") ||
                    PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("Micromass PKL File") ||
                    PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("mzXML") ||
                    PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("mzML") ||
                    PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("mzData") ||
                    PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("VEMS") ||
                    PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("Mascot Generic File") ||
                    PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("MS2")) {
                new SpectraSelectionNoIdentifications(this.getLocation());
            } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("X!Tandem") ||
                    PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("OMSSA") ||
                    PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("Mascot DAT File")) {
                new SpectraSelectionWithIdentifications(this.getLocation());
            }

            this.setVisible(false);
            this.dispose();

            this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        }
    }//GEN-LAST:event_nextJButtonActionPerformed

    /**
     * @see #cancelJButtonActionPerformed(java.awt.event.ActionEvent)
     * 
     * @param evt
     */
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        cancelJButtonActionPerformed(null);
    }//GEN-LAST:event_formWindowClosing

    /**
     * Opens a Help dialog.
     * 
     * @param evt
     */
    private void helpJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpJButtonActionPerformed
        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        new HelpDialog(this, false, getClass().getResource("/no/uib/prideconverter/helpfiles/DataFileSelection.html"));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_helpJButtonActionPerformed

    /**
     * Opens a About PRIDE Convertr dialog.
     * 
     * @param evt
     */
    private void aboutJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutJButtonActionPerformed
        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        new HelpDialog(this, false, getClass().getResource("/no/uib/prideconverter/helpfiles/AboutPRIDE_Converter.html"));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_aboutJButtonActionPerformed

    /**
     * Closes the frame and opens the previous frame (DataSourceSelection).
     * 
     * @param evt
     */
    private void backJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backJButtonActionPerformed
        new DataSourceSelection(this.getLocation());
        this.setVisible(false);
        this.dispose();
    }//GEN-LAST:event_backJButtonActionPerformed

    /**
     * Deletes the selected rows in the selected source files table.
     * 
     * @param evt
     */
    private void deleteJMenuItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteJMenuItemActionPerformed
        if (selectedFilesJTable.getSelectedRow() != -1) {

            PRIDEConverter.getProperties().setSpectrumTableModel(null);

            int[] selectedRows = selectedFilesJTable.getSelectedRows();

            for (int i = selectedRows.length - 1; i >= 0; i--) {
                PRIDEConverter.getProperties().getSelectedSourceFiles().remove(selectedRows[i]);

                ((DefaultTableModel) selectedFilesJTable.getModel()).removeRow(selectedRows[i]);
            }

            fixIndices();
            nextJButton.setEnabled(selectedFilesJTable.getRowCount() > 0);
        }
    }//GEN-LAST:event_deleteJMenuItemActionPerformed

    /**
     * If right clicking a "Delete selected rows" popup menu is shown.
     * 
     * @param evt
     */
    private void selectedFilesJTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_selectedFilesJTableMouseClicked
        if (evt.getButton() == MouseEvent.BUTTON3) {

            int row = selectedFilesJTable.rowAtPoint(evt.getPoint());
            int column = selectedFilesJTable.columnAtPoint(evt.getPoint());

            int[] selectedRows = selectedFilesJTable.getSelectedRows();

            if (selectedRows.length > 0) {
                if (row < selectedRows[0] || row >
                        selectedRows[selectedRows.length - 1]) {
                    selectedFilesJTable.changeSelection(row, column, false, false);
                }
            } else {
                selectedFilesJTable.changeSelection(row, column, false, false);
            }

            deleteSelectedFileJPopupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_selectedFilesJTableMouseClicked

    /**
     * Opens a file chooser that lets the user select the file to include in 
     * the conversion. Uses file filters to only display/select the correct 
     * file types.
     * 
     * @param evt
     */
    private void selectFileOrFolderJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectFileOrFolderJButtonActionPerformed

        // ToDo: Lots of code is repeated. Should be rewritten/simplified.

        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        JFileChooser chooser = new JFileChooser(PRIDEConverter.getUserProperties().getCurrentSourceFileLocation());
        chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        chooser.setMultiSelectionEnabled(true);

        if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("Mascot Generic File")) {
            chooser.setFileFilter(new MgfFileFilter());
        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("Mascot DAT File")) {
            chooser.setFileFilter(new DatFileFilter());
        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("SEQUEST DTA File")) {
            chooser.setFileFilter(new DtaFileFilter());
        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("X!Tandem")) {
            chooser.setFileFilter(new XmlFileFilter());
        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("Micromass PKL File")) {
            chooser.setFileFilter(new PklFileFilter());
        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("mzXML")) {
            chooser.setFileFilter(new MzXmlFileFilter());
        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("OMSSA")) {
            chooser.setFileFilter(new OmxFileFilter());
        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("mzML")) {
            chooser.setFileFilter(new MzMlFileFilter());
        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("mzData")) {
            chooser.setFileFilter(new MzDataFileFilter());
        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("VEMS")) {
            chooser.setFileFilter(new PkxFileFilter());
        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("MS2")) {
            chooser.setFileFilter(new Ms2FileFilter());
        }

        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            String path;

            PRIDEConverter.getProperties().setSpectrumTableModel(null);
            PRIDEConverter.getProperties().setInstrumentDetailsExtracted(false);
            PRIDEConverter.getProperties().setSelectedSourceFiles(new ArrayList());
            PRIDEConverter.getProperties().setSpectraSelectionCriteria(null);
            PRIDEConverter.getProperties().setDataFileHasBeenLoaded(false);
            PRIDEConverter.getProperties().setSampleDetailsExtracted(false);
            PRIDEConverter.getProperties().setContactInfoExtracted(false);
            PRIDEConverter.getProperties().setCurrentQuantificationSelection(new ArrayList());
            PRIDEConverter.getProperties().setSpectrumCvParams(new HashMap<String, ArrayList<CvParam>>());
            PRIDEConverter.getProperties().setSpectrumUserParams(new HashMap<String, ArrayList<UserParam>>());

            while (selectedFilesJTable.getRowCount() > 0) {
                ((DefaultTableModel) selectedFilesJTable.getModel()).removeRow(0);
            }

            PRIDEConverter.getUserProperties().setSourceFileLocation(chooser.getSelectedFile().getPath());

            File[] allFiles = chooser.getSelectedFiles();

            for (int j = 0; j < allFiles.length; j++) {

                File currentFile = allFiles[j];

                if (currentFile.isDirectory()) {

                    File[] files = currentFile.listFiles();
                    File tempFile;

                    for (int i = 0; i < files.length; i++) {
                        tempFile = files[i];

                        if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("Mascot Generic File")) {
                            if (tempFile.getAbsolutePath().endsWith(".mgf") ||
                                    tempFile.getAbsolutePath().endsWith(".MGF")) {

                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this, 
                                            "A file called " + tempFile.getPath() + " is already selected.",
                                            "File Already Selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("Mascot DAT File")) {
                            if (tempFile.getAbsolutePath().endsWith(".dat") ||
                                    tempFile.getAbsolutePath().endsWith(".DAT")) {
                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this, 
                                            "A file called " + tempFile.getPath() + " is already selected.",
                                            "File Already Selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("SEQUEST DTA File")) {
                            if (tempFile.getAbsolutePath().endsWith(".dta") ||
                                    tempFile.getAbsolutePath().endsWith(".DTA")) {
                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this, 
                                            "A file called " + tempFile.getPath() +
                                            " is Already Selected.", "File already selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("X!Tandem")) {
                            if (tempFile.getAbsolutePath().endsWith(".xml") ||
                                    tempFile.getAbsolutePath().endsWith(".XML")) {
                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this, 
                                            "A file called " + tempFile.getPath() + " is already selected.",
                                            "File Already Selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("Micromass PKL File")) {
                            if (tempFile.getAbsolutePath().endsWith(".pkl") ||
                                    tempFile.getAbsolutePath().endsWith(".PKL")) {
                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this, 
                                            "A file called " + tempFile.getPath() + " is already selected.",
                                            "File Already Selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("mzXML")) {
                            if (tempFile.getAbsolutePath().endsWith(".mzXML") ||
                                    tempFile.getAbsolutePath().endsWith(".MZXML") ||
                                    tempFile.getAbsolutePath().endsWith(".mzxml")) {
                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this, 
                                            "A file called " + tempFile.getPath() + " is already selected.",
                                            "File Already Selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("OMSSA")) {
                            if (tempFile.getAbsolutePath().endsWith(".omx") ||
                                    tempFile.getAbsolutePath().endsWith(".OMX")) {
                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this, 
                                            "A file called " + tempFile.getPath() + " is already selected.",
                                            "File Already Selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("mzML")) {
                            if (tempFile.getAbsolutePath().endsWith(".mzML") ||
                                    tempFile.getAbsolutePath().endsWith(".MZML") ||
                                    tempFile.getAbsolutePath().endsWith(".mzml")) {
                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this, 
                                            "A file called " + tempFile.getPath() + " is already selected.",
                                            "File Already Selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("mzData")) {
                            if (tempFile.getAbsolutePath().endsWith(".mzDATA") ||
                                    tempFile.getAbsolutePath().endsWith(".MZDATA") ||
                                    tempFile.getAbsolutePath().endsWith(".mzdata")) {
                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this,
                                            "A file called " + tempFile.getPath() + " is already selected.",
                                            "File Already Selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("VEMS")) {
                            if (tempFile.getAbsolutePath().endsWith(".pkx") ||
                                    tempFile.getAbsolutePath().endsWith(".PKX")) {
                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this, 
                                            "A file called " +  tempFile.getPath() + " is already selected.",
                                            "File Already Selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        } else if (PRIDEConverter.getProperties().getDataSource().equalsIgnoreCase("MS2")) {
                            if (tempFile.getAbsolutePath().endsWith(".ms2") ||
                                    tempFile.getAbsolutePath().endsWith(".MS2")) {
                                if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(tempFile.getPath())) {
                                    ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                            new Object[]{
                                                new Integer(selectedFilesJTable.getRowCount() +
                                                1),
                                                tempFile.getName(),
                                                new Boolean(true)
                                            });

                                    PRIDEConverter.getProperties().getSelectedSourceFiles().add(tempFile.getPath());
                                } else {
                                    JOptionPane.showMessageDialog(this, 
                                            "A file called " + tempFile.getPath() + " is already selected.",
                                            "File Already Selected", JOptionPane.ERROR_MESSAGE);
                                }
                            }
                        }
                    }
                } else {

                    if (!PRIDEConverter.getProperties().getSelectedSourceFiles().contains(
                            currentFile.getPath())) {
                        ((DefaultTableModel) selectedFilesJTable.getModel()).addRow(
                                new Object[]{
                                    new Integer(selectedFilesJTable.getRowCount() + 1),
                                    currentFile.getName(),
                                    new Boolean(true)
                                });

                        PRIDEConverter.getProperties().getSelectedSourceFiles().add(currentFile.getPath());
                    } else {
                        JOptionPane.showMessageDialog(this, 
                                "A file called " + currentFile.getPath() + " is already selected.",
                                "File Already Selected", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        }

        nextJButton.setEnabled(selectedFilesJTable.getRowCount() > 0);

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_selectFileOrFolderJButtonActionPerformed

    /**
     * If the "Delete" button is clicked the selected rows are delted from 
     * the table.
     * 
     * @param evt
     */
    private void selectedFilesJTableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_selectedFilesJTableKeyReleased
        if (evt.getKeyCode() == KeyEvent.VK_DELETE) {
            deleteJMenuItemActionPerformed(null);
        }
    }//GEN-LAST:event_selectedFilesJTableKeyReleased

    /**
     * Fixes the indices in the table so that they are in accending 
     * order starting from one.
     */
    private void fixIndices() {
        for (int row = 0; row <
                ((DefaultTableModel) selectedFilesJTable.getModel()).getRowCount(); row++) {
            ((DefaultTableModel) selectedFilesJTable.getModel()).setValueAt(new Integer(row +
                    1), row, 0);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aboutJButton;
    private javax.swing.JButton backJButton;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancelJButton;
    private javax.swing.JMenuItem deleteJMenuItem;
    private javax.swing.JPopupMenu deleteSelectedFileJPopupMenu;
    private javax.swing.JButton helpJButton;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton nextJButton;
    private javax.swing.JButton selectFileOrFolderJButton;
    private javax.swing.JTable selectedFilesJTable;
    // End of variables declaration//GEN-END:variables
}
